import instance from "./request.js";

// 通过 ip 地址查询所在城市
/**
 *    
 * @returns {
    "status": "1",
    "info": "OK",
    "infocode": "10000",
    "province": "湖北省",
    "city": "武汉市",
    "adcode": "420100",
    "rectangle": "114.0169501,30.36460893;114.6261227,30.77017755"
}
 */
export function getCityByIp() {
  return instance({
    method: 'GET',
    url: '/ip',
    params: {
      key: '5a84c1f402300f480bd90df3ddf12355'  // 替换为你的高德地图 API key
    }
  })
}

// 通过城市名查询 adcode 
/**
 *
 * @param {string} address  --城市名 输入'武汉市'
 * @returns {
    "status": "1",
    "info": "OK",
    "infocode": "10000",
    "count": "1",
    "geocodes": [
        {
            "formatted_address": "湖北省武汉市",
            "country": "中国",
            "province": "湖北省",
            "citycode": "027",
            "city": "武汉市",
            "district": [],
            "township": [],
            "neighborhood": {
                "name": [],
                "type": []
            },
            "building": {
                "name": [],
                "type": []
            },
            "adcode": "420100",
            "street": [],
            "number": [],
            "location": "114.304569,30.593354",
            "level": "市"
        }
    ]
}
 */ 
export function getCityByName(address) {
  return instance({
    method: 'GET',
    url: 'geocode/geo',
    params: {
      key: '5a84c1f402300f480bd90df3ddf12355',
      address: address
    }
  })
}

// 获取天气信息
/**
 *
 * @param {*} adcode --城市的 adcode
 * @param {*} extensions --可选 base：返回实况天气 可选 all:返回预报天气
 * @returns 
 * {  --base：返回实况天气
    "status": "1",
    "count": "1",
    "info": "OK",
    "infocode": "10000",
    "lives": [
        {
            "province": "湖北",
            "city": "武汉市",
            "adcode": "420100",
            "weather": "晴",
            "temperature": "34",
            "winddirection": "西南",
            "windpower": "≤3",
            "humidity": "54",
            "reporttime": "2025-08-25 10:31:11",
            "temperature_float": "34.0",
            "humidity_float": "54.0"
        }
    ]
}
* { --all:返回预报天气
    "status": "1",
    "count": "1",
    "info": "OK",
    "infocode": "10000",
    "forecasts": [
        {
            "city": "武汉市",
            "adcode": "420100",
            "province": "湖北",
            "reporttime": "2025-08-25 10:31:11",
            "casts": [
                {
                    "date": "2025-08-25",
                    "week": "1",
                    "dayweather": "多云",
                    "nightweather": "多云",
                    "daytemp": "38",
                    "nighttemp": "27",
                    "daywind": "北",
                    "nightwind": "北",
                    "daypower": "1-3",
                    "nightpower": "1-3",
                    "daytemp_float": "38.0",
                    "nighttemp_float": "27.0"
                },
                {
                    "date": "2025-08-26",
                    "week": "2",
                    "dayweather": "多云",
                    "nightweather": "多云",
                    "daytemp": "38",
                    "nighttemp": "27",
                    "daywind": "北",
                    "nightwind": "北",
                    "daypower": "1-3",
                    "nightpower": "1-3",
                    "daytemp_float": "38.0",
                    "nighttemp_float": "27.0"
                },
                {
                    "date": "2025-08-27",
                    "week": "3",
                    "dayweather": "晴",
                    "nightweather": "晴",
                    "daytemp": "38",
                    "nighttemp": "28",
                    "daywind": "北",
                    "nightwind": "北",
                    "daypower": "1-3",
                    "nightpower": "1-3",
                    "daytemp_float": "38.0",
                    "nighttemp_float": "28.0"
                },
                {
                    "date": "2025-08-28",
                    "week": "4",
                    "dayweather": "晴",
                    "nightweather": "晴",
                    "daytemp": "38",
                    "nighttemp": "28",
                    "daywind": "北",
                    "nightwind": "北",
                    "daypower": "1-3",
                    "nightpower": "1-3",
                    "daytemp_float": "38.0",
                    "nighttemp_float": "28.0"
                }
            ]
        }
    ]
}
 */
export function getWeatherByAdcode(adcode, extensions='base') {
  return instance({
    method: 'GET',
    url: 'weather/weatherInfo',
    params: {
      key: '5a84c1f402300f480bd90df3ddf12355',
      city: adcode,
      extensions: extensions
    }
  })
}

